package com.qxsx.midi.activity.personal;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.qxsx.midi.R;
import com.qxsx.midi.activity.base.BaseActivity;
import com.qxsx.midi.activity.main.MainActivity;
import com.qxsx.midi.config.AppContext;
import com.qxsx.midi.utils.MyNet;
import com.qxsx.midi.utils.SPUtils;
import com.qxsx.midi.utils.callBackJsonArray;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.regex.Pattern;

/**
 * Created by LYJ on 2015/9/8.
 */
public class PersonalEditEmailActivity extends BaseActivity implements View.OnClickListener{
    private RelativeLayout rl_mess,rl_name,rl_email,rl_position,rl_password,rl_phonenum;
    private LinearLayout ll_back,ll_save;
    private EditText ed_email;
    Intent intent = new Intent();
    String userid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personal_email);
        setTitle("邮箱");
        setNavBtn(R.mipmap.back, null, 0, "保存");
//        ll_back = (LinearLayout)findViewById(R.id.name_back);
//        ll_save = (LinearLayout)findViewById(R.id.name_save);
        ed_email = (EditText)findViewById(R.id.ed_name_name);
        String tv_email = (String)SPUtils.get(AppContext.getContext(),"user_eamil","");
        if(tv_email==null){
            tv_email="";
        }
        ed_email.setText(tv_email);
//        ll_back.setOnClickListener(this);
//        ll_save.setOnClickListener(this);
        userid = (String) SPUtils.get(AppContext.getContext(), "user_id", "");

    }

    @Override
    protected void HandleTitleBarEvent(TitleBar component, View v) {
        switch (component) {
            case LEFT:
                finish();
                break;
            case RIGHT:
                final String email= ed_email.getText().toString().trim();
                if(email!=null&&email!=""){
                    if(isEmail(email)){
                        JSONArray array = new JSONArray();
                        JSONObject object = new JSONObject();
                        try {
                            object.put("user_id",userid);
                            object.put("eamil", email);
                            array.put(object);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                        MyNet.doPostJsonArray("106", array, new callBackJsonArray() {
                                    @Override
                                    public void run(Object code, Object message, JSONArray array) {
                                       // Log.e("-------------",code.toString());
                                        if (code.toString().equals("401")) {
                                            Toast.makeText(AppContext.getContext(), message.toString(), Toast.LENGTH_SHORT).show();
                                        } else if (array == null) {
                                            Log.w("array", array + "");
                                            return;
                                        } else {
                                            SPUtils.put(AppContext.getContext(), "user_email", email);
                                            Toast.makeText(AppContext.getContext(), message.toString(), Toast.LENGTH_SHORT).show();
                                            finish();
                                        }
                                    }
                                }

                        );
//                        finish();
                    }else {
                        Toast.makeText(getApplicationContext(), "邮箱格式不正确", Toast.LENGTH_LONG).show();
                    }

                }
               // finish();
                break;
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
//            case R.id.name_back:
//                //返回
//                intent.setClass(AppContext.getContext(),PersonalDetailsActivity.class);
//                startActivity(intent);
//                finish();
//                break;
//            case R.id.name_save:
//                //保存邮箱
//                String email= ed_email.getText().toString().trim();
//                if(email!=null&&email!=""){
//                    if(isEmail(email)){
//                        intent.setClass(AppContext.getContext(),PersonalDetailsActivity.class);
//                        startActivity(intent);
//                        finish();
//                    }else {
//                        Toast.makeText(getApplicationContext(), "邮箱格式不正确", Toast.LENGTH_LONG).show();
//                    }
//
//                }
//                break;
        }
    }

    public boolean isEmail(String email) {

        String str = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
        Pattern p = Pattern.compile(str);
        return p.matcher(email).matches();

    }

    public static int dip2px(Context context, float dipValue){
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int)(dipValue * scale + 0.5f);
    }

    // 像素转 dpi
    public static int px2dip(Context context, float pxValue){
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int)(pxValue / scale + 0.5f);
    }
}
